// pages/refundCause/refundCause.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //获取手机高度
    that.setData({
      navH: app.globalData.navHeight,
      orderId: options.orderId,
      productId: options.productId,
      opasid: options.opasid,
      opId: options.opId,
    });
    wx.request({
      url: app.globalData.url + 'getParaById?paraId=open_to_booking_rules',
      method:"get",
      success:function(res){
        that.setData({
          cause:res.data.data,
        })
      }
    })
    
    
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getOrderDetails();
    wx.request({
      url: app.globalData.url + 'getProductDetailByProductId?productId=' + that.data.productId,
      method: "get",
      success: function (res) {
        that.setData({
          product: res.data.data,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  navBack: function () {
    wx.navigateBack({

    })
  },
  countTime: function (edTime) {
    //var edTime ='2020-02-21 22:33:27';
    var that = this;
    var date = new Date();
    var now = date.getTime();
    var endDate = new Date(that.data.edTime.replace(/\.|\-/g, '/'));//设置截止时间
    var end = endDate.getTime();
    var leftTime = end - now; //时间差                              
    var d, h, m, s, ms;
    if (leftTime >= 0) {
      d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
      h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
      m = Math.floor(leftTime / 1000 / 60 % 60);
      s = Math.floor(leftTime / 1000 % 60);
      ms = Math.floor(leftTime % 1000);
      ms = ms < 100 ? "0" + ms : ms
      s = s < 10 ? "0" + s : s
      m = m < 10 ? "0" + m : m
      h = h < 10 ? "0" + h : h
      that.setData({
        countdown: d + "天" + h + "时" + m + "分" + s + "秒",
      })
      //递归每秒调用countTime方法，显示动态时间效果
      setTimeout(that.countTime, 100);
    } else {
      console.log('已截止')
      that.setData({
        countdown: '00:00:00'
      })
    }
  },
  //获取几天后的日期
  getDateStr: function (today, addDayCount) {
    console.info(addDayCount);
    var dd;
    if (today) {
      dd = new Date(today);
    } else {
      dd = new Date();
    }
    dd.setDate(parseInt(dd.getDate()) + parseInt(addDayCount));//获取AddDayCount天后的日期 
    var y = dd.getFullYear();
    var month = dd.getMonth() + 1;//获取当前月份的日期 
    var d = dd.getDate();
    var h = dd.getHours();
    var m = dd.getMinutes();
    var s = dd.getSeconds();
    if (m < 10) {
      m = '0' + m;
    };
    if (d < 10) {
      d = '0' + d;
    };
    return y + "-" + month + "-" + d + " " + h + ':' + m + ':' + s;
  },
  information:function(e){
    wx.navigateTo({
      url: '/pages/refundInformation/refundInformation?opasid=' + e.currentTarget.dataset.opasid,
    })
  }, 
  userIntegralOrder:function(e){
    var that = this;
    wx.request({
      url: app.globalData.url + "userCancelApplicationForRefund",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        opasId: e.currentTarget.dataset.opasid,
        opId: that.data.opId,
      },
      method: "POST",
      success: function (res) {
        var data = res.data;
        if (data.result === 1) {
          that.getOrderDetails();
        }
      }
    }); 
  },
  //加载
  getOrderDetails:function(){
    var that = this;
    wx.request({
      url: app.globalData.url + 'getAfterSaleDetail',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        id: that.data.opasid
      },
      success: function (res) {
        that.setData({
          after: res.data.data,
        })
        if (that.data.after.status == 1) {
          wx.request({
            url: app.globalData.url + 'getParaById?paraId=order_product_refund_audit',
            method: "get",
            success: function (res) {
              var edTime = that.getDateStr(null, res.data.data.value);
              that.setData({
                edTime: edTime
              })
              that.countTime(that.data.edTime);
            }
          })
        }
        if (that.data.after.type == 3) {//退款
          wx.request({
            url: app.globalData.url + 'getParaById?paraId=refund_user_explain',
            method: "get",
            success: function (res) {
              var str = res.data.data.value.split("\\n");
              that.setData({
                refundCause: str,
              })
            }
          })
        } else if (that.data.after.type == 2) {//换货
          wx.request({
            url: app.globalData.url + 'getParaById?paraId=exchange_goods_user_explain',
            method: "get",
            success: function (res) {
              var str = res.data.data.value.split("\\n");
              that.setData({
                refundCause: str,
              })
            }
          })
        } else if (that.data.after.type == 1) {//退换以及退款
          wx.request({
            url: app.globalData.url + 'getParaById?paraId=refund_price_user_explain',
            method: "get",
            success: function (res) {
              var str = res.data.data.value.split("\\n");
              that.setData({
                refundCause: str,
              })
            }
          })
        }
      }
    })

    // wx.request({
    //   url: app.globalData.url + 'getOrderDetails?orderId=' + that.data.orderId,
    //   method: "get",
    //   success: function (res) {
    //     that.setData({
    //       order: res.data.data,
    //       status1: res.data.data.afterSafeList[0].status
    //     })
    //     if (res.data.data.afterSafeList[0].status == 1) {
    //       wx.request({
    //         url: app.globalData.url + 'getParaById?paraId=order_product_refund_audit',
    //         method: "get",
    //         success: function (res) {
    //           var edTime = that.getDateStr(null, res.data.data.value);
    //           that.setData({
    //             edTime: edTime
    //           })
    //           that.countTime(that.data.edTime);
    //         }
    //       })
    //     }
    //     if (res.data.data.afterSafeList[0].type == 3) {//退款
    //       wx.request({
    //         url: app.globalData.url + 'getParaById?paraId=refund_user_explain',
    //         method: "get",
    //         success: function (res) {
    //           var str = res.data.data.value.split("\\n");
    //           that.setData({
    //             refundCause: str,
    //           })
    //         }
    //       })
    //     } else if (res.data.data.afterSafeList[0].type == 2) {//换货
    //       wx.request({
    //         url: app.globalData.url + 'getParaById?paraId=exchange_goods_user_explain',
    //         method: "get",
    //         success: function (res) {
    //           var str = res.data.data.value.split("\\n");
    //           that.setData({
    //             refundCause: str,
    //           })
    //         }
    //       })
    //     } else if (res.data.data.afterSafeList[0].type == 1) {//退换以及退款
    //       wx.request({
    //         url: app.globalData.url + 'getParaById?paraId=refund_price_user_explain',
    //         method: "get",
    //         success: function (res) {
    //           var str = res.data.data.value.split("\\n");
    //           that.setData({
    //             refundCause: str,
    //           })
    //         }
    //       })
    //     }
    //   }
    // })
  },
  refundCourier:function(e){
    var that = this;
    wx.navigateTo({
      url: '/pages/refundCourier/refundCourier?opasId=' + e.currentTarget.dataset.opasid,
    })
  },
})